package neusoft.example.com.yuejian06.Activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import neusoft.example.com.yuejian06.Bean.Club;
import neusoft.example.com.yuejian06.Fragment.ClubListFragment;
import neusoft.example.com.yuejian06.Fragment.ClubvideoListFragment;
import neusoft.example.com.yuejian06.Fragment.CourseListFragment;
import neusoft.example.com.yuejian06.Model.ClubDetailModel;
import neusoft.example.com.yuejian06.PublicIp;
import neusoft.example.com.yuejian06.R;
import neusoft.example.com.yuejian06.RetrofitListener;

public class ClubDetailActivity extends AppCompatActivity {
    private ImageView club_return,attention_club,club_pic;
    private TextView club_name,club_intro,club_tel,club_address,club_opentime,club_closetime;
    private Intent intent;
    private RetrofitListener<Club> listener;
    private ClubDetailModel model;
    private String user_id,club_id;
    private SharedPreferences sp;

    private Button button_clubvideo,button_course;
    private LinearLayout clubvideo,course;
    private ViewPager viewPager;
    private List<Fragment> listfragment;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private FragmentPagerAdapter fragmentPagerAdapter;
    private int select = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_detail);

        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            //actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        }
        select = getIntent().getIntExtra("selectTab",0);
        initView();
        //club_name1=getIntent().getStringExtra("club_name");

        listener();

        initEvents();
        initFragment();
        resetImage();
        selectTab(select);


        model=new ClubDetailModel();
        model.getClubById(club_id,listener);
    }
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {



            switch (view.getId()) {
                case R.id.button_clubvideo:

                    selectTab(0);
                    break;
                case R.id.button_course:

                    selectTab(1);

                    break;
            }
        }
    };

    public void initView(){
        club_return=findViewById(R.id.club_return);
        attention_club=findViewById(R.id.attention_club);
        club_pic=findViewById(R.id.club_pic);
        club_name=findViewById(R.id.club_name);
        club_intro= findViewById(R.id.club_intro);
        club_tel=findViewById(R.id.club_tel);
        club_address=findViewById(R.id.club_address);
        club_opentime=findViewById(R.id.club_opentime);
        club_closetime=findViewById(R.id.club_closetime);


        sp=getSharedPreferences("user_id",MODE_PRIVATE);
        user_id=sp.getString("user_id",null);
        club_id = getIntent().getStringExtra("club_id");



        button_clubvideo = (Button) findViewById(R.id.button_clubvideo);
        button_course = (Button) findViewById(R.id.button_course);

        clubvideo = (LinearLayout) findViewById(R.id.clubvideo);
        course = (LinearLayout) findViewById(R.id.course);

        viewPager = (ViewPager) findViewById(R.id.viewPager);

    }
    public void initEvents(){
        club_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent=new Intent(ClubDetailActivity.this,ClubListFragment.class);
                startActivity(intent);
            }
        });

        button_clubvideo.setOnClickListener(onClickListener);
        button_course.setOnClickListener(onClickListener);
    }
   private void listener(){
       listener=new RetrofitListener<Club>() {
           @Override
           public void onResponse(Club l) {
               Picasso.with(ClubDetailActivity.this)
                       .load(PublicIp.ip+"static/club_picture/"+l.getClub_pic())
                       .resize(200,100)
                       .into(club_pic);
               club_name.setText(l.getClub_name());
               club_intro.setText(l.getClub_intro());
               club_tel.setText(l.getClub_tel());
               club_address.setText(l.getClub_address());
               club_opentime.setText(l.getClub_opentime());
               club_closetime.setText(l.getClub_closetime());

           }

           @Override
           public void onFail(String msg) {
               Toast.makeText(ClubDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
           }
       };
   }
    private void initFragment() {
        listfragment = new ArrayList<Fragment>();
        listfragment.add(new ClubvideoListFragment(club_id));
        listfragment.add(new CourseListFragment(club_id));

        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }

            @Override
            public int getCount() {
                return listfragment.size();
            }
        };
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                System.out.println("----" + String.valueOf(position));
                viewPager.setCurrentItem(position);
                resetImage();
                selectTab(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void resetImage(){
        button_clubvideo.setBackgroundColor(Color.parseColor("#f2f2f2"));
        button_course.setBackgroundColor(Color.parseColor("#f2f2f2"));
    }

    private void selectTab(int i) {
        switch (i) {
            case 0:
                clubvideo.setBackgroundColor(Color.parseColor("#ffcccc"));
                button_clubvideo.setBackgroundColor(Color.parseColor("#ffcccc"));

                break;
            case 1:
                course.setBackgroundColor(Color.parseColor("#ffcccc"));
                button_course.setBackgroundColor(Color.parseColor("#ffcccc"));
                break;

        }
        viewPager.setCurrentItem(i);
    }


}
